package cn.itcast.web.servlet;

import cn.itcast.domain.Req;
import cn.itcast.service.FileService;
import cn.itcast.service.ReqService;
import cn.itcast.service.UserService;
import cn.itcast.service.impl.FileServiceImpl;
import cn.itcast.service.impl.ReqServiceImpl;
import cn.itcast.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/updateReqInfoServlet")
public class UpdateReqInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1设置编码
        request.setCharacterEncoding("utf-8");
        //1获取参数
        String id = request.getParameter("id");
        String yid = request.getParameter("yid");
        String info=request.getParameter("info");
        Map<String, String[]> map = request.getParameterMap();
        Req req=new Req();
        try {
            BeanUtils.populate(req,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //调用service方法
        ReqService service=new ReqServiceImpl();
       service.updateReqInfo(id,yid,info);
       //调用FileService
        FileService service1=new FileServiceImpl();
        service1.userUpdateReq(yid,id,info);
       request.setAttribute("yid",yid);
//        response.sendRedirect(request.getContextPath()+"/findReqByYidServlet");
        request.getRequestDispatcher("/findReqByYidServlet").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
